package com.resou.reader.view.booklebel.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.Toast;

import com.google.gson.internal.LinkedTreeMap;
import com.resou.reader.R;
import com.resou.reader.api.entry.BookLabelBean;
import com.resou.reader.utils.GsonUtils;
import com.resou.reader.view.booklebel.LabelBean;
import com.resou.reader.view.booklebel.LabelNameBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description: 标签显示的适配器
 * CreateTime: 2019-03 -23
 **/
public class LabelAdapter extends RecyclerView.Adapter<LabelAdapter.ViewHolder> {

    Context context;
    List<BookLabelBean.FilterDetailBean> list;  //数据源 显示的标签
    ILabelResultCallBack iLabelResultCallBack; //点击标签的接口回调
    List<BookLabelBean.FilterDetailBean> resultList;  //选中后 存放的集合

    /**
     * 点击回调
     *
     * @param iLabelResultCallBack
     */
    public void setiLabelResultCallBack(ILabelResultCallBack iLabelResultCallBack) {
        this.iLabelResultCallBack = iLabelResultCallBack;
    }

    public LabelAdapter(Context context, List<BookLabelBean.FilterDetailBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_item_label, viewGroup, false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
        viewHolder.cbLabel.setText(list.get(position).getTypeDesc());
        if (list.get(position).isCheck()) {
            viewHolder.cbLabel.setChecked(true);
        } else {
            viewHolder.cbLabel.setChecked(false);
        }
        viewHolder.cbLabel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != iLabelResultCallBack) {
                    if (viewHolder.cbLabel.isChecked()) {
                        iLabelResultCallBack.setOnclickCancellCallBack(list.get(position));
                    } else {
                        iLabelResultCallBack.setOnclickReselutCallBack(list.get(position));
                    }
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        CheckedTextView cbLabel;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cbLabel = itemView.findViewById(R.id.cbLabel);
        }
    }

    /**
     * 数据更新
     *
     * @param list
     */
    public void updataData(List<BookLabelBean.FilterDetailBean> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    /**
     * 数据清除
     */
    public void clear() {
        if (this.list != null) {
            this.list.clear();
            this.notifyDataSetChanged();
        }
    }

    public interface ILabelResultCallBack {
        /**
         * 选中后的监听
         *
         * @param labelBean
         */
        void setOnclickReselutCallBack(BookLabelBean.FilterDetailBean labelBean);

        /**
         * 取消选中的监听
         *
         * @param labelBean
         */
        void setOnclickCancellCallBack(BookLabelBean.FilterDetailBean labelBean);
    }
}
